<template>
  <div id="home">
    <!-- 标题栏 -->
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <!-- 选项卡（吸顶） -->
    <tab-control
      ref="tabControl1"
      class="tab-control-fixed"
      :titles="['流行', '新款', '精选']"
      v-show="isTabFixed"
      @tabClick="tabClick"
    />
    <!-- 滚动区 -->
    <scroll
      class="home-scroll"
      ref="scroll"
      :probeType="3"
      :pullUpLoad="true"
      @scroll="homeScroll"
      @pullingUp="loadMore"
    >
      <!-- 轮播图 -->
      <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad" />
      <!-- 推荐 -->
      <recommend-view :recommends="recommends" />
      <!-- 专题 -->
      <feature-view />
      <!-- 选项卡 -->
      <tab-control
        ref="tabControl2"
        :titles="['流行', '新款', '精选']"
        @tabClick="tabClick"
      />
      <!-- 商品列表 -->
      <goods-list :goods="showGoods" />
    </scroll>
    <!-- 返回顶部按钮 -->
    <back-top v-show="isShowBackTop" @click.native="backTop" />
  </div>
</template>

<script>
import NavBar from "components/common/navbar/NavBar";
import Scroll from "components/common/scroll/Scroll";

import TabControl from "components/content/tabControl/TabControl";
import GoodsList from "components/content/goods/GoodsList";

import HomeSwiper from "./childComps/HomeSwiper";
import RecommendView from "./childComps/RecommendView";
import FeatureView from "./childComps/FeatureView";

import { itemListener, backTop } from "common/mixin";

import { getHomeMultidata, getHomeGoods } from "network/home";

export default {
  components: {
    NavBar,
    Scroll,
    TabControl,
    GoodsList,
    HomeSwiper,
    RecommendView,
    FeatureView
  },
  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] }
      },
      curTabType: "pop",
      tabOffsetTop: 0,
      isTabFixed: false,
      scrollY: 0
    };
  },
  computed: {
    showGoods() {
      return this.goods[this.curTabType].list;
    }
  },
  mixins: [itemListener, backTop],
  created() {
    // 1.请求多个数据
    this.getHomeMultidata();
    // 2.请求商品列表数据
    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
  },
  activated() {
    console.log("Home activated---" + this.scrollY);
    this.$refs.scroll.refresh(); // 若返回Home时，展示的位置不对，可加此代码试试
    this.$refs.scroll.scrollTo(0, this.scrollY);
  },
  deactivated() {
    // 记录滚动的y值
    this.scrollY = this.$refs.scroll.getScrollY();
    // 取消全局事件的监听
    this.$bus.$off("goodsItemImgLoad", this.itemImgListener);
  },
  methods: {
    /**
     * 网络请求相关方法
     */
    getHomeMultidata() {
      getHomeMultidata().then(res => {
        let { banner, recommend } = res.data;
        this.banners = banner.list;
        this.recommends = recommend.list;
      });
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then(res => {
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page += 1;
      });
    },
    /**
     * 事件监听相关方法
     */
    tabClick(index) {
      /**
       * 选项卡点击事件
       */
      switch (index) {
        case 0:
          this.curTabType = "pop";
          break;
        case 1:
          this.curTabType = "new";
          break;
        case 2:
          this.curTabType = "sell";
          break;
      }
      // 保持tabControl选中项一致
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index;
    },
    homeScroll(position) {
      // 返回顶部
      this.showBackTopListener(position);
      // 决定tabControl是否吸顶
      this.isTabFixed = -position.y > this.tabOffsetTop;
    },
    loadMore() {
      console.log("上拉加载更多");
      this.getHomeGoods(this.curTabType);
      this.$refs.scroll.finishPullUp();
    },
    swiperImageLoad() {
      /**
       * 组件都有一个$el属性，用于获取组件中的元素；
       * 问题：获取的offsetTop值不正确，因为其上有图片，若图片加载完成的慢，会导致获取错误的值；
       * 解决：监听图片加载完成事件，并获取值
       */
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop;
    }
  }
};
</script>

<style scoped>
#home {
  height: 100vh;
}
.home-nav {
  color: #fff;
  background-color: var(--color-tint);
}
.tab-control-fixed {
  position: relative;
  z-index: 9;
}
.home-scroll {
  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
  overflow: hidden;
}
</style>
